[Grails] How to enlist children of specified Parent in treeview colum (table)

Posted by Rehman on Stack Overflow See other posts from Stack Overflow or by Rehman
Published on 2010-12-27T11:12:46Z Indexed on 2010/12/27 11:53 UTC
Read the original article Hit count: 265

I am newbie in grails and tried to implement treeview using RichUI plugin, which shows all parents with individual children in Parent.list.gsp

xml for parent and their children

     <parents name='Parents'>
  <Parent id='1' name='Parent_1'>
    <Children name='Children'>
      <child name='Child_2' id='2' />
      <child name='Child_4' id='4' />
      <child name='Child_1' id='3' />
      <child name='Child_3' id='1' />
    </Children>
  </Parent>
  <Parent id='2' name='Parent_2'>
    <Children name='Children'>
      <child name='Child_1' id='8' />
      <child name='Child_2' id='7' />
      <child name='Child_4' id='6' />
      <child name='Child_3' id='5' />
    </Children>
  </Parent>
</parents>

Parent Domain Class

class Parent {

String name

static hasMany = [children:Child]

}

Child Domain Class

class Child {

    String name
    Parent parent

    static belongsTo = [parent:Parent]

}

Parent Controller

def list = {

    def writer = new StringWriter()
    def xml = new MarkupBuilder(writer)
    xml.parents(name: "Parents"){
        Parent.list().each {
            Parent parentt = it
            Parent( id:parentt.id,name:parentt.name) {
                Children(name:'Children'){
                    parentt.children.each {
                        Child childd = it
                        child(name:childd.name,id:childd.id)
                    }
                }
            }
        }
    }
    if(!params.max)params.max=10
    ["data":writer.toString(),parentInstanceList: Parent.list(params), parentInstanceTotal: Parent.count()]
}

Parent.list.gsp

 <head>
    <resource:treeView/> ...</head>

 <body>
   <table>
                <thead>
                    <tr>

                        <g:sortableColumn property="id" title="${message(code: 'parent.id.label', default: 'Id')}" />

                        <g:sortableColumn property="name" title="${message(code: 'parent.name.label', default: 'Name')}" />
                        <g:sortableColumn property="relationship" title="${message(code: 'parent.relationhsip.label', default: 'Relationship')}" />

                    </tr>
                </thead>
                <tbody>
                <g:each in="${parentInstanceList}" status="i" var="parentInstance">
                    <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">

                        <td><g:link action="show" id="${parentInstance.id}">${fieldValue(bean: parentInstance, field: "id")}</g:link></td>
                        <td>${fieldValue(bean: parentInstance, field: "name")}</td>
                        <td><richui:treeView  xml="${data}" /></td>

                    </tr>
                </g:each>
                </tbody>
            </table>

 </body>

Problem

Currently, in list view, every Parent entry has list of all parents and their children under relationship column

Parent List view Snapshot link text

Question

how can i enlist all children only for each parent instead of enlisting all parents with their children in each Parent entry ?

thanks in advance

Rehman

© Stack Overflow or respective owner

Related posts about grails

Related posts about grails-plugin